package com.mxpio.erp.sales.controller;


import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.sales.entity.Address;
import com.mxpio.erp.common.sales.service.AddressService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "AddressController", description = "发货地址数据接口")
@RestController
@RequestMapping("/erp/sales/address/")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("list")
    @Operation(summary = "发货地址列表", description = "获取发货地址列表（无分页）", method = "GET")
    public Result<List<Address>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<Address> addresses = addressService.list(Address.class, criteria);
        return Result.OK(addresses);
    }

    @GetMapping("page")
    @Operation(summary = "发货地址列表", description = "获取发货地址列表（分页）", method = "GET")
    public Result<Page<Address>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<Address> addresss = addressService.listPage(Address.class, page, criteria);
        return Result.OK(addresss);
    }
    
    @GetMapping("list/bypn/{pnCode}")
    @Operation(summary = "根据pnCode获取发货地址", description = "根据pnCode获取发货地址", method = "GET")
    public Result<List<Address>> listByPnCode(@Parameter(description="伙伴编码") @PathVariable(name = "pnCode", required = true) String pnCode) {
        Criteria c = Criteria.create();
        c.addCriterion("pnCode", Operator.EQ, pnCode);
        return Result.OK(addressService.list(Address.class, c));
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取发货地址", description = "根据ID获取发货地址", method = "GET")
    public Result<Address> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        Address address = addressService.getById(Address.class, id);
        return Result.OK(address);
    }

    @PostMapping("add")
    @Operation(summary = "新增发货地址", description = "新增发货地址", method = "POST")
    public Result<Address> add(@RequestBody Address address) {
        addressService.save(address);
        return Result.OK(address);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑发货地址", description = "编辑发货地址", method = "PUT")
    public Result<Address> edit(@RequestBody Address address) {
        addressService.update(address);
        return Result.OK(address);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除发货地址", description = "删除发货地址,多个id用英文逗号分隔", method = "DELETE")
    public Result<Address> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            addressService.remove(Address.class, key);
        }
        return Result.OK();
    }
}
